package references

import (
	"gvm/instructions/base"
	"gvm/rtda"
	"gvm/rtda/heap"
)

type NEW struct {
	base.Index16Instruction
}

func (n *NEW) Execute(jFrame *rtda.JFrame) {
	cp := jFrame.Method().Class().ConstantPool()
	classRef := cp.GetConstant(n.Index).(*heap.ClassRef)
	class := classRef.ResolveClass()

	if !class.InitStarted() {
		jFrame.RevertNextPC()
		base.InitClass(jFrame.JThread(), class)
		return
	}

	if class.IsInterface() || class.IsAbstract() {
		panic("java.lang.InstantiationError")
	}
	ref := class.NewObject()
	println(class.Name())
	jFrame.OperandStack().PushRef(ref)
}
